package com.itheima.manager.controller;

import com.itheima.manager.domain.Student;
import com.itheima.manager.service.StudentService;

import java.util.Scanner;

/**
 * @Author chris
 * @Date 2020/6/15 21:22
 */
public class StudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();


    public void start() {
        while (true){
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice){
                case "1":
                    addStu();
                    break;
                case "2":
                    deleteStu();
                    break;
                case "3":
                    updateStu();
                    break;
                case "4":
                    findAllStu();
                    break;
                case "5":
                    return;

                default:
                    System.out.println("输入数据识别失败!请重输....");

            }
        }

    }

    private void addStu() {
        System.out.println("请输入ID...");
        String ID;
        while (true){
            ID = sc.next();
            boolean exist=studentService.isExist(ID);
            if(exist){
                System.out.println("ID已存在,请重新输入...");
                continue;
            }
            break;
        }

        System.out.println("请输入姓名...");
        String name = sc.next();
        System.out.println("请输入年龄...");
        String age = sc.next();
        System.out.println("请输入生日...");
        String birth = sc.next();
        Student stu = new Student(name,ID,age,birth);

        boolean result=studentService.addStu(stu);
        if(!result){
            System.out.println("数据存储已满,添加失败!");
        }else {
            System.out.println("添加成功!");
        }
    }
    private void findAllStu() {
        Student[] stus=studentService.findAllStu();
        System.out.println("---------------------------------------------");
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            if(stus[i]!=null){
                System.out.println(stus[i].getID()+"\t\t"+stus[i].getName()+"\t\t"+
                        stus[i].getAge()+"\t\t"+stus[i].getBirth());
            }
        }
        System.out.println("---------------------------------------------");


    }

    private void updateStu() {
        System.out.println("请输入需要操作学员学号...");
        String ID;
        while (true){
            ID = sc.next();
            boolean exist = studentService.isExist(ID);
            if(!exist){
                System.out.println("需要操作的学员不存在请重输!");
                continue;
            }
            break;
        }

        System.out.println("请输入姓名...");
        String name = sc.next();
        System.out.println("请输入年龄...");
        String age = sc.next();
        System.out.println("请输入生日...");
        String birth = sc.next();
        Student stu = new Student(name,ID,age,birth);

        studentService.updateStu(stu);
        System.out.println("修改成功");

    }

    private void deleteStu() {
        System.out.println("请输入需要操作学员学号...");
        String ID;
        while (true){
            ID = sc.next();
            boolean exist = studentService.isExist(ID);
            if(!exist){
                System.out.println("需要操作的学员不存在请重输!");
                continue;
            }
            break;
        }

        studentService.deleteStu(ID);
        System.out.println("删除成功");

    }


}
